using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MecRobGest.GUI
{
    public partial class GUIMantFacturas : Form
    {
        public GUIMantFacturas()
        {
            InitializeComponent();
        }

        private void facturaBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.facturaBindingSource.EndEdit();
            this.facturaTableAdapter.Update(this.dBDataset.Factura);

        }

        private void GUIMantFacturas_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dBDataset.Facturas' table. You can move, or remove it, as needed.
            this.facturasTableAdapter1.Fill(this.dBDataset.Facturas);
            // TODO: This line of code loads data into the 'dBDataset.Factura' table. You can move, or remove it, as needed.
            this.facturaTableAdapter.Fill(this.dBDataset.Factura);
            // TODO: This line of code loads data into the 'dBDataset.Facturas' table. You can move, or remove it, as needed.
            
            // TODO: This line of code loads data into the 'dBDataset.Factura' table. You can move, or remove it, as needed.
            this.facturaTableAdapter.Fill(this.dBDataset.Factura);
            // TODO: This line of code loads data into the 'dBDataset.Facturas' table. You can move, or remove it, as needed.
            
            // TODO: This line of code loads data into the 'dBDataset.Factura' table. You can move, or remove it, as needed.
            this.facturaTableAdapter.Fill(this.dBDataset.Factura);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MecRobGest.Database.Controllers.DBDataset.FacturasDataTable f = facturasTableAdapter1.GetData();
            for (int i = 0; i < f.Rows.Count; i++){
                int factura = 0;
                textBox1.AppendText((int)f.Rows[i].ItemArray[0] + " ");
                factura = (int)f.Rows[i].ItemArray[1];
                textBox1.AppendText((int)f.Rows[i].ItemArray[1] + " ");
                textBox1.AppendText((int)f.Rows[i].ItemArray[2] + " ");
                textBox1.AppendText((bool)f.Rows[i].ItemArray[3] + " ");
                textBox1.AppendText((double)f.Rows[i].ItemArray[4] + " ");
                textBox1.AppendText(facturasTableAdapter1.getClientFromFactura(factura));
                textBox1.AppendText(f.Rows[i].ItemArray[5].ToString() + " ");
                textBox1.AppendText(f.Rows[i].ItemArray[6].ToString() + "\r\n");
            }
            
            for (int i = 0; i < f.Rows.Count; i++){
                int id = ((int)f.Rows[i].ItemArray[0] );
                int nFact = ((int)f.Rows[i].ItemArray[1]);
                int nAlb =((int)f.Rows[i].ItemArray[2] );
                bool bapplyDiscount = ((bool)f.Rows[i].ItemArray[3]);
                double discount = ((double)f.Rows[i].ItemArray[4]);
                DateTime fechaPago = DateTime.MinValue;
                if(!f.Rows[i].ItemArray[5].ToString().Trim().Equals(""))
                    fechaPago = DateTime.Parse(f.Rows[i].ItemArray[5].ToString());
                DateTime fechaEmision = DateTime.MinValue;
                if(!f.Rows[i].ItemArray[6].ToString().Trim().Equals(""))
                    fechaEmision = DateTime.Parse(f.Rows[i].ItemArray[6].ToString());
                String client = facturasTableAdapter1.getClientFromFactura(nFact);
                try{
                    facturaTableAdapter.InsertFactura(nFact,bapplyDiscount,(Decimal)discount,fechaPago,fechaEmision,client);
                }catch(Exception err){
                    textBox1.AppendText(id + " --> No insertat \r\n");
                }
            }
        }

        private void facturaBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
        {
            this.Validate();
            this.facturaBindingSource.EndEdit();
            this.facturaTableAdapter.Update(this.dBDataset.Factura);

        }

        private void facturaBindingNavigatorSaveItem_Click_2(object sender, EventArgs e)
        {
            this.Validate();
            this.facturaBindingSource.EndEdit();
            this.facturaTableAdapter.Update(this.dBDataset.Factura);

        }
    }
}